/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.cgb.portal.bean;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author LUCAS
 */
public class AppBaseBean {

    protected static final String FIELD_BLANK = "";
    protected static final int FIELD_BLANK_INTEGER = Integer.MAX_VALUE;
    protected static final boolean FIELD_BLANK_BOOLEAN = false;
    protected static final String DOMAIN = "http://localhost:8080//portal//";
    protected static final String RESOURCE_NAME = "br.cgb.portal.properties.language";

    protected FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    protected void addErrorMessage(String alvo, String titulo, String mensagem) {
        getFacesContext().addMessage(alvo, new FacesMessage(FacesMessage.SEVERITY_ERROR, titulo, mensagem));
    }

    protected void addInfoMessage(String alvo, String titulo, String mensagem) {
        getFacesContext().addMessage(alvo, new FacesMessage(FacesMessage.SEVERITY_INFO, titulo, mensagem));
    }

    protected boolean isPostBack() {
        return this.getFacesContext().isPostback();
    }

    protected ExternalContext getExternalContext() {
        return this.getFacesContext().getExternalContext();
    }

    protected HttpServletRequest getRequest() {
        return (HttpServletRequest) this.getExternalContext().getRequest();
    }

    protected HttpServletResponse getResponse() {
        return (HttpServletResponse) this.getExternalContext().getResponse();
    }

    protected HttpSession getSession() {
        return this.getRequest().getSession();
    }

    protected HttpSession getSession(boolean create) {
        return this.getRequest().getSession(create);
    }

    protected void setLanguage(Locale locale) {
        this.getFacesContext().getViewRoot().setLocale(locale);
    }

    protected Locale getLanguage() {
        return this.getFacesContext().getViewRoot().getLocale();
    }

    protected ResourceBundle getBundle(){
        return ResourceBundle.getBundle(RESOURCE_NAME, getLanguage());
    }
    
    protected String getRealPath(String path) {
        return this.getSession().getServletContext().getRealPath(path);
    }

    protected void redirect(String url) {
        try {
            this.getFacesContext().getExternalContext().redirect(DOMAIN + url);
        } catch (Exception e) {
        }
    }

    protected static String getDOMAIN() {
        return DOMAIN;
    }
}
